<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Search.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\community;

use app\api\model\dao\Circle;
use app\api\model\dao\Note;
use app\api\model\dao\WordLibrary;
use app\api\model\data\note\NoteData;
use app\library\Common;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Log;
use think\Validate;

class Search extends PageBase
{

    private $objCircle;
    private $objNote;
    private $objNoteData;
    private $objWordLibrary;

    public function __construct()
    {
        parent::__construct();

        $this->objCircle = new Circle();
        $this->objNote = new Note();
        $this->objWordLibrary = new WordLibrary();
        $this->objNoteData = new NoteData();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'wd' => 'require',
        ];
        $msg = [
            'wd.require' => '请输入查询内容',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {

        $ret = $this->checkParam($param);
        if ($ret) {
            Log::error(__METHOD__ . ' param error. [param: ' . json_encode($param) . ']');
            return $this->getRes(Error::ERR_PARAM);
        }

        $wd = $param['wd'];

        // 对输入内容切分
        $words = Common::scws($wd);

        if (empty($words)) {
            $words[] = $wd;
        }

        $search_ret = [];

        if (!empty($words)) {
            foreach ($words as $word) {
                $ret = $this->objWordLibrary->getByWord($word);
                $search_ret = array_merge($search_ret, $ret);

                $ret = $this->objNote->searchNote($word);
                if (!empty($ret)) {
                    foreach ($ret as $val) {
                        $search_ret[] = [
                            'type' => Flag::CONST_WORD_TYPE_NOTE,
                            'obj_id' => $val['id']
                        ];
                    }
                }
            }
        }

        $note_id = [];

        if (!empty($search_ret)) {
            foreach ($search_ret as $search) {
                if (Flag::CONST_WORD_TYPE_NOTE == $search['type']) {
                    $note_id[] = $search['obj_id'];
                }
            }
        }

        if (!empty($note_id)) {
            $note_id = array_unique($note_id);
            $arrNote = $this->objNote->getNoteByids($note_id, ['id', 'title', 'content']);
        }

        $data = [];

        if (!empty($arrNote)) {
            foreach ($arrNote as $val) {
                $data[] = [
                    'note_id' => $val['id'],
                    'title' => $val['title'],
                    'content' => $val['content'],
                ];
            }
        }

        $this->data['list'] = $data;

        return $this->getRes();

    }
}
